﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using JXT.PrimaryKey.Batman.Core.Configuration;
using JXT.PrimaryKey.Batman.Core.Infrastructure;

namespace JXT.PrimaryKey.Batman.Domain.Repositories.Mongo
{
    /// <summary>
    /// MongoDB数据库存储上下文对象
    /// </summary>
    public class MongoRepositoryContext : RepositoryContext
    {
        private static MongoServer _connection;
        private static string _connectionString;
        private static string _databaseName;

        private static readonly object _synLock = new object();

        /// <summary>
        /// 连接Url
        /// </summary>
        public string ConnectionString { get { return _connectionString; } }

        /// <summary>
        /// 数据库名
        /// </summary>
        public string DatabaseName { get { return _databaseName; } }

        /// <summary>
        /// 当前连接
        /// </summary>
        public MongoServer Connection
        {
            get { return _connection as MongoServer; }
        }

        public MongoDatabase Database { get; private set; }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="connectionName">数据库连接名称</param>
        public MongoRepositoryContext(string connectionName)
        {
            //string connectionName = "default";
            ConfigurationName = connectionName;
            if (_connection == null)
            {
                lock (_synLock)
                {
                    if (_connection == null)
                    {
                        var configurationSection = SupernovaConfigurationManager.GetServiceConfigurationSection();
                        if (configurationSection != null)
                        {
                            if (String.IsNullOrEmpty(connectionName) || connectionName.Equals("default", StringComparison.InvariantCultureIgnoreCase))
                            {
                                _connectionString = configurationSection.ConnectionSetting.DefaultConnectionString;
                            }
                            else
                            {
                                var connectionSetting = configurationSection.ConnectionSetting[connectionName];
                                if (connectionSetting == null)
                                    _connectionString = configurationSection.ConnectionSetting.DefaultConnectionString;
                                else
                                {
                                    _connectionString = connectionSetting.ConnectionString;
                                    _databaseName = connectionSetting.Database;
                                }
                            }
                        }
                        //MongoConnectionStringBuilder _connectionBuilder = new MongoConnectionStringBuilder(_connectionString);
                        //_connectionBuilder.ConnectTimeout = TimeSpan.FromSeconds(30);
                        //_connectionBuilder.SocketTimeout = TimeSpan.FromSeconds(300);
                        //_connectionBuilder.MaxConnectionPoolSize = 1000;
                        _connection = MongoServer.Create(_connectionString);
                    }
                }
            }
            Database = Connection.GetDatabase(DatabaseName);
        }

        public override void BeginTransaction()
        {
            throw new NotSupportedException();
        }

        public override void CommitTransaction()
        {
            throw new NotSupportedException();
        }

        public override void RollbackTransaction()
        {
            throw new NotSupportedException();
        }

        public override void DisposeTransaction()
        {
            throw new NotSupportedException();
        }

        public override void Dispose()
        {
            //base.Dispose();
            if (Connection != null)
            {
                //Connection.Disconnect();
            }
        }

        ~MongoRepositoryContext()
        {
            if (_connection != null)
            {
                _connection.Disconnect();
                _connection = null;
            }
        }
    }
}
